const fs = require('fs') // 创建文件或文件夹
const cheerio = require('cheerio') // cheerio爬虫
const iconv = require('iconv-lite') // 编码
const request = require('request') //利用request模块保存图片

// 目标网站
const baseUrl = "https://www.qidian.com"
// htttps
const https = "https:"
// 小说保存路径
const filePath = "book/"

// 书籍名称
const query = "迎娶魔道妖女开始"

searchBookList(query)

// 查询书籍列表
function searchBookList(query) {
    const url = baseUrl + "/so/" + encodeURI(query);
    request({
        url: url,
        method: "GET",
        encoding: null//获取的内容不编码，二进制
    }, function (error, response, buffer) {
        let html = iconv.decode(buffer, 'UTF-8').toString();
        searchBook(html)
    });
}

// 查询书籍
function searchBook(html) {
    // console.log(html)
    if (html) {
        const $ = cheerio.load(html, { decodeEntities: false });
        $('#result-list .book-img-text ul li').each(function (index, elem) {
            let bookName = $(elem).find('.book-info-title').text()
            let chapterListUrl = https + $(elem).find('.book-info-title a').attr('href')
            console.log($(elem).find('.book-info-title').text() + '-' + $(elem).find('.author .name').text())
            console.log("网址：" + chapterListUrl)

            // 删除空格等特殊字符
            bookName = filePath + bookName.replace(/(\s)+/g, "")

            // 创建文件夹
            fs.exists(bookName, function (exists) {
                if (exists) {
                    console.log("该文件存在！");
                }
                else {
                    // 创建文件夹
                    fs.mkdir(bookName, err => {
                        if (err) {
                            console.log('创建文件夹失败', err);
                            return;
                        }
                        console.log('创建文件夹成功');
                    })
                }
            });

            // 进入章节目录
            request({
                url: chapterListUrl,
                method: "GET",
                encoding: null//获取的内容不编码，二进制
            }, function (error, response, buffer) {
                html = iconv.decode(buffer, 'utf-8').toString()
                searchChapter(bookName, html)
            });

            // 跳出循环
            return false
        })
    }
}

// 查询章节列表
function searchChapter(bookName, html) {
    // console.log(html)
    if (html) {
        const $ = cheerio.load(html, { decodeEntities: false });
        let free = $('#allCatalog .catalog-volume')[0]
        $(free).find("ul li").each(function (index, elem) {
            let chapterName = $(elem).text().trim()

            // 格式化章节名
            chapterName = chapterName.replace(/(\s)+/g, "")
            chapterName = chapterName.replace(/(\?)+/g, "")

            let chapterUrl = https + $(elem).find('a').attr('href')
            console.log(chapterName)
            console.log("网址：" + chapterUrl)

            // 进入章节
            request({
                url: chapterUrl,
                method: "GET",
                encoding: null//获取的内容不编码，二进制
            }, function (error, response, buffer) {
                html = iconv.decode(buffer, 'utf-8').toString();
                // console.log(html)
                getContent(bookName, chapterName, html)
            });

            // 跳出循环
            // return false
        })
    }
}

function getContent(bookName, chapterName, html) {
    // console.log(html)
    if (html) {
        const $ = cheerio.load(html, { decodeEntities: false });
        let fileName = bookName + "/" + chapterName + '.txt'
        let content = $('main').html().trim()

        // 格式化内容
        content = content.replace(/(<p>)+/g, "wrap")
        content = content.replace(/(<\/p>)+/g, "wrap")
        content = content.replace(/(wrap)+/g, "\n\n")

        // console.log(content)
        // 保存文件
        fs.writeFile(fileName, content, (err) => {
            if (err) throw err;
            console.log('文本文件已创建并写入数据');
        });
    }
}